<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/17 0017
 * Time: 下午 3:03
 */

namespace app\admin\controller;

use app\admin\model\Indent as IndentModel;
use app\admin\model\Product;
use think\Db;
use think\Request;

class Indent extends Common
{
    /**
     * @title   订单列表
     */
    public function index()
    {
        $where=[];
        if(Request::instance()->isGet()){
            $key = input('get.key');
            switch ($key){
                case 'p_name':
                    $where['i_type'] = input('get.type');
                    $where[$key] = array('like',"%".input('get.value')."%");
                    break;
                case 'i_phone':
                    $where['i_type'] = input('get.type');
                    $where[$key] = array('like',"%".input('get.value')."%");
            }
        }
//        $where['i_confirm'] = 0;
        $where['complete']  = 1;
        $list = Db::name('indent')->alias('i')->where($where)->join('product p','i.i_pid=p.id','left')->order('i.create_time desc')->paginate(15);
        $this->assign('page',$list->render());
        $list = $list -> toarray();
        foreach ($list['data'] as $k => $v){
            $list['data'][$k]['id'] = Db::name('indent')->where('i_number',$v['i_number'])->value('id');
            $list['data'][$k]['p_id'] = Db::name('user')->where('id',$v['p_id'])->value('u_nickname');
            $list['data'][$k]['dealer_id'] = Db::name('user')->where('id',$v['dealer_id'])->value('u_nickname');
            $list['data'][$k]['u_nickname'] = Db::name('user')->where('id',$v['i_uid'])->value('u_nickname');
        }
//        dump($list);die;
        $this->assign('list',$list['data']);
        return $this->fetch('admin/indent/index');
    }

    /**
     * @title   已发货订单列表
     * @return mixed
     */
    public function delivery()
    {
        $where='';
        if(Request::instance()->isGet()){
            $key = input('get.key');
            switch ($key){
                case 'p_name':
                    $where['i_type'] = input('get.type');
                    $where[$key] = array('like',"%".input('get.value')."%");
                    break;
                case 'i_phone':
                    $where['i_type'] = input('get.type');
                    $where[$key] = array('like',"%".input('get.value')."%");
            }
        }
        $where['i_confirm'] = 1;
        $list = Db::name('indent')->alias('i')->where($where)->join('product p','i.i_pid=p.id','left')->order('i.create_time desc')->paginate(15);
        $this->assign('page',$list->render());
        $list = $list -> toarray();
        foreach ($list['data'] as $k => $v){
            $list['data'][$k]['id'] = Db::name('indent')->where('i_number',$v['i_number'])->value('id');
            $list['data'][$k]['p_id'] = Db::name('user')->where('id',$v['p_id'])->value('u_nickname');
            $list['data'][$k]['dealer_id'] = Db::name('user')->where('id',$v['dealer_id'])->value('u_nickname');
            $list['data'][$k]['u_nickname'] = Db::name('user')->where('id',$v['i_uid'])->value('u_nickname');
        }
        $this->assign('list',$list['data']);
        return $this->fetch('admin/indent/index_delivery');
    }

    /**
     * @title   订单详情
     * @param null $id  订单id
     * @return mixed
     */
    public function detail(&$id = null)
    {
        $list = IndentModel::get($id);
        $list['product'] = (new Product())->where('id',$list['i_pid'])->find();
        $this->assign('list',$list);

        $pay_order = Db::name('pay_order')->where('indentid',$id)->select();
        $this->assign('paylist',$pay_order);

        $express = Db::name('logistics')->where('indent_id',$id)->find();
        if($express) {
            $this->assign('express', $express);
            $express_company = config('express_company');
            $this->assign('express_company', $express_company[$express['express_name']]);
        }
        return $this->fetch('admin/indent/detail');
    }

    /**
     * @title   填写物流信息
     */
    public function express()
    {
        $data = Request::instance()->param();
        if(isset($data['id'])) {
            $add = Db::name('logistics')->update($data);
            if($add) {
                $this->success('修改物流成功');
            }
            else{
                $this->error('修改物流失败');
            };
        }
        else  {
            $add = Db::name('logistics')->insert($data);
            if($add) {
                //修改订单为已发货
                (new IndentModel())->where('id',$data['indent_id'])->setField('i_confirm',1);

                $this->success('添加物流成功');
            }
            else{
                $this->error('添加物流失败');
            };
        }


    }

    /**
     * @title   客服修改订单地址
     * @param null $id
     */
    public function editAddress(&$id=null)
    {
        $data = Request::instance()->param();
        $data['id'] = $id;
        $ret = $this->store((new IndentModel()),$data);
        if($ret['status']=='success'){
            $this->success($ret['msg']);
        }else{
            $this->error($ret['msg']);
        }
    }
}